package com.eatwhat.controller;

import com.eatwhat.dto.ApiResponse;
import com.eatwhat.entity.UserChoice;
import com.eatwhat.service.UserChoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/choices")
@CrossOrigin
public class UserChoiceController {
    
    @Autowired
    private UserChoiceService userChoiceService;
    
    @GetMapping("/recent")
    public ApiResponse<List<UserChoice>> getRecentChoices(HttpServletRequest request) {
        String userId = getClientId(request);
        List<UserChoice> choices = userChoiceService.getRecentChoices(userId);
        return ApiResponse.success(choices);
    }
    
    @GetMapping("/today")
    public ApiResponse<List<UserChoice>> getTodayChoices(HttpServletRequest request) {
        String userId = getClientId(request);
        List<UserChoice> choices = userChoiceService.getTodayChoices(userId);
        return ApiResponse.success(choices);
    }
    
    @GetMapping("/popular")
    public ApiResponse<List<Object>> getPopularFoods(@RequestParam(defaultValue = "7") int days) {
        List<Object> popularFoods = userChoiceService.getPopularFoods(days);
        return ApiResponse.success(popularFoods);
    }
    
    private String getClientId(HttpServletRequest request) {
        String xForwardedFor = request.getHeader("X-Forwarded-For");
        String clientIp = xForwardedFor != null ? xForwardedFor.split(",")[0] : request.getRemoteAddr();
        return clientIp;
    }
}